<template>
  <div class="profile">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <h3>个人信息</h3>
        </div>
      </template>
      
      <div v-if="user" class="profile-content">
        <el-descriptions :column="1" border>
          <el-descriptions-item label="用户名">{{ user.username }}</el-descriptions-item>
          <el-descriptions-item label="电子邮箱">{{ user.email }}</el-descriptions-item>
          <el-descriptions-item label="角色">
            <el-tag 
              v-for="role in user.roles" 
              :key="role" 
              :type="role === 'ROLE_ADMIN' ? 'danger' : 'info'"
              class="role-tag"
            >
              {{ formatRole(role) }}
            </el-tag>
          </el-descriptions-item>
        </el-descriptions>
        
        <div class="form-container">
          <h4>修改密码</h4>
          <el-form
            ref="passwordFormRef"
            :model="passwordForm"
            :rules="passwordRules"
            label-width="120px"
            class="password-form"
          >
            <el-form-item label="当前密码" prop="currentPassword">
              <el-input 
                v-model="passwordForm.currentPassword" 
                type="password" 
                placeholder="请输入当前密码" 
                show-password
              />
            </el-form-item>
            
            <el-form-item label="新密码" prop="newPassword">
              <el-input 
                v-model="passwordForm.newPassword" 
                type="password" 
                placeholder="请输入新密码" 
                show-password
              />
            </el-form-item>
            
            <el-form-item label="确认新密码" prop="confirmPassword">
              <el-input 
                v-model="passwordForm.confirmPassword" 
                type="password" 
                placeholder="请再次输入新密码" 
                show-password
              />
            </el-form-item>
            
            <el-form-item>
              <el-button type="primary" @click="changePassword" :loading="loading">
                修改密码
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      
      <div v-else class="empty-data">
        <el-empty description="未找到用户信息" />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '@/store/auth'

const authStore = useAuthStore()
const user = ref(null)
const loading = ref(false)
const passwordFormRef = ref(null)

// 密码表单
const passwordForm = reactive({
  currentPassword: '',
  newPassword: '',
  confirmPassword: ''
})

// 密码表单验证规则
const passwordRules = {
  currentPassword: [
    { required: true, message: '请输入当前密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6个字符', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 格式化角色
const formatRole = (role) => {
  switch (role) {
    case 'ROLE_ADMIN': return '管理员'
    case 'ROLE_USER': return '员工'
    default: return role
  }
}

// 修改密码
const changePassword = async () => {
  if (!passwordFormRef.value) return
  
  await passwordFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        await authStore.changePassword({
          currentPassword: passwordForm.currentPassword,
          newPassword: passwordForm.newPassword
        })
        
        ElMessage.success('密码修改成功')
        
        // 清空表单
        passwordForm.currentPassword = ''
        passwordForm.newPassword = ''
        passwordForm.confirmPassword = ''
        
        // 重置表单验证状态
        passwordFormRef.value.resetFields()
      } catch (error) {
        console.error('修改密码失败:', error)
        ElMessage.error('修改密码失败: ' + (error.message || '请检查当前密码是否正确'))
      } finally {
        loading.value = false
      }
    } else {
      ElMessage.error('请检查表单填写是否正确')
      return false
    }
  })
}

// 初始化
onMounted(() => {
  user.value = authStore.user
})
</script>

<style scoped>
.profile {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.profile-content {
  max-width: 800px;
}

.form-container {
  margin-top: 30px;
}

.password-form {
  max-width: 500px;
  margin-top: 20px;
}

.role-tag {
  margin-right: 5px;
}

.empty-data {
  padding: 40px 0;
  display: flex;
  justify-content: center;
}
</style> 